package com.cray.intelligenthydrologyback.web;

import com.cray.intelligenthydrologyback.client.domain.entity.IndexModel;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

/**
 * @author Emil
 */
@Controller
@ResponseBody
@RequestMapping("/index")
@Api(value = "Index Controller", tags = "首页管理")
public class IndexController {
    @Autowired
    private WebClient.Builder webClientBuilder;
    @Autowired
    private ObjectMapper objectMapper;

    @GetMapping("/countTotal")
    @ApiOperation(httpMethod = "GET", value = "首页查询传感器、要素、摄像头、算法")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Add system face info",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public IndexModel getCountTotal() throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().get()  // 使用 POST 请求
                .uri("/index/countTotal")  // 请求的 URL
                .retrieve() // 发送请求并获取响应
                .bodyToMono(String.class); // 响应内容转换为 String
        return objectMapper.readValue(response.block(), IndexModel.class);
    }
}
